﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Telerik.Web.UI;

public partial class SiteWorx_WebParts_Advanced_RssFeedLink : BaseWebPart
{
    private string _channelName = "";
    private string _navigateUrl = "";
    private string _target = "";
    private string _text = "Rss";
    private string _hoverText = "";
    private bool _displayImage = true;
    private bool _rssVisible = true;
    private HorizontalAlign _rssLinkHorizontalAlign = HorizontalAlign.NotSet;

    [Personalizable(PersonalizationScope.Shared)]
    public string ChannelName
    {
        get
        {
            return _channelName;
        }
        set
        {
            _channelName = value;
        }
    }

    [Personalizable(PersonalizationScope.Shared)]
    public string NavigateUrl
    {
        get
        {
            return _navigateUrl;
        }
        set
        {
            _navigateUrl = value;
        }
    }

    [Personalizable(PersonalizationScope.Shared)]
    public string Target
    {
        get
        {
            return _target;
        }
        set
        {
            _target = value;
        }
    }

    [Personalizable(PersonalizationScope.Shared)]
    public string Text
    {
        get
        {
            return _text;
        }
        set
        {
            _text = value;
        }
    }

    [Personalizable(PersonalizationScope.Shared)]
    public string HoverText
    {
        get
        {
            return _hoverText;
        }
        set
        {
            _hoverText = value;
        }
    }

    [Personalizable(PersonalizationScope.Shared)]
    public bool DisplayImage
    {
        get
        {
            return _displayImage;
        }
        set
        {
            _displayImage = value;
        }
    }

    [Personalizable(PersonalizationScope.Shared)]
    public bool RssVisible
    {
        get
        {
            return _rssVisible;
        }
        set
        {
            _rssVisible = value;
        }
    }

    [Personalizable(PersonalizationScope.Shared)]
    public HorizontalAlign RssLinkHorizontalAlign
    {
        get
        {
            return _rssLinkHorizontalAlign;
        }
        set
        {
            _rssLinkHorizontalAlign = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindRssFeedLink();
            pnlEditContentButton.Visible = Page.User.IsInRole("Administrator") && !Utility.PageInPreviewMode();
        }
    }

    private void BindRssFeedLink()
    {
        ctlRssFeedLink.ChannelName = ChannelName;
        ctlRssFeedLink.NavigateUrl = NavigateUrl;
        ctlRssFeedLink.Target = Target;
        pnlRssLink.HorizontalAlign = RssLinkHorizontalAlign;

        if (RssVisible)
        {
            ctlRssFeedLink.Text = Text;
            ctlRssFeedLink.ToolTip = HoverText;

            if (DisplayImage)
            {
                ctlRssFeedLink.ImageUrl = "~/Rss/Images/feed.gif";
                litRssLink.Text = "&nbsp;";
                hlnkRssToolkit.Text = Text;
                hlnkRssToolkit.NavigateUrl = NavigateUrl;
                hlnkRssToolkit.ToolTip = HoverText;
                hlnkRssToolkit.Target = Target;
                litRssLink.Visible = true;
                hlnkRssToolkit.Visible = true;
            }
            else
            {
                litRssLink.Visible = false;
                hlnkRssToolkit.Visible = false;
            }
        }
        else
        {
             ctlRssFeedLink.Text = "";
             litRssLink.Visible = false;
             hlnkRssToolkit.Text = "";
        }
    }

    protected void btnDelete_Click(object sender, EventArgs e)
    {
        RemoveWebPart();
    }

    protected override void RemoveWebPart()
    {
        base.RemoveWebPart();
    }

    protected void lnkbtnUpdateRssFeedLinkWPData_Click(object sender, EventArgs e)
    {
        string rssFeedLinkWPDataId = hidRssFeedLinkWPDataId.Value;
        RssFeedLinkWebPartData data = (RssFeedLinkWebPartData)Session[rssFeedLinkWPDataId];
        this.ChannelName = data.ChannelName;
        this.NavigateUrl = data.NavigateUrl;
        this.Target = data.Target;
        this.Text = data.Text;
        this.HoverText = data.HoverText;
        this.DisplayImage = data.DisplayImage;
        this.RssVisible = data.RssVisible;
        this.RssLinkHorizontalAlign = data.RssLinkHorizontalAlign;

        //clear the content id and session variable
        hidRssFeedLinkWPDataId.Value = "";
        Session[rssFeedLinkWPDataId] = null;

        BindRssFeedLink();
    }

    protected void btnEdit_Click(object sender, EventArgs e)
    {
        string rssFeedLinkWPDataId = Guid.NewGuid().ToString() + "_RssFeedLink";
        Session[rssFeedLinkWPDataId] = new RssFeedLinkWebPartData(this.ChannelName, this.NavigateUrl, this.Target, this.Text,
                                                                  this.HoverText, this.DisplayImage, this.RssVisible, this.RssLinkHorizontalAlign);
        hidRssFeedLinkWPDataId.Value = rssFeedLinkWPDataId;

        Telerik.Web.UI.RadWindow rssFeedLinkEditor = new Telerik.Web.UI.RadWindow();
        rssFeedLinkEditor.NavigateUrl = "~/SiteWorx/WebParts/Advanced/RssFeedLink/RssFeedLinkEditor.aspx?rssFeedLinkWPDataId=" + rssFeedLinkWPDataId;
        rssFeedLinkEditor.VisibleOnPageLoad = true;
        rssFeedLinkEditor.Modal = true;
        rssFeedLinkEditor.Behaviors = WindowBehaviors.Move | WindowBehaviors.Resize;
        rssFeedLinkEditor.ShowContentDuringLoad = false;
        rssFeedLinkEditor.VisibleStatusbar = false;
        rssFeedLinkEditor.Width = Unit.Pixel(585);
        rssFeedLinkEditor.Height = Unit.Pixel(450);

        pnlEditContentButton.Controls.Add(rssFeedLinkEditor);

        BuildUpdateScript();
    }

    private void BuildUpdateScript()
    {
        string rssFeedLinkWPDataId = hidRssFeedLinkWPDataId.Value;
        if (rssFeedLinkWPDataId.Length > 0)
        {
            string functionName = "Update_" + rssFeedLinkWPDataId.Replace("-", "_");
            string script = "function " + functionName + "(sender){__doPostBack('" + lnkbtnUpdateRssFeedLinkWPData.UniqueID + "','')}";
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), functionName, script, true);
        }
    }
}